package com.tianqu.tms.www.bid.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.AucBidhistory;
import com.tianqu.tms.entity.AucManagement;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.bid.viewhelper.BidCarSerchAllBidDetailViewHelper;

public class BidCarSerchBidDetailController extends TmsController {
	private Logger logger = Logger
			.getLogger(BidCarSerchBidDetailController.class);
	private static String SERIAL = "serial";
	private static String ID = "id";
	BidCarSerchAllBidDetailViewHelper view = new BidCarSerchAllBidDetailViewHelper();

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String serial = (String) uc.getAttribute(SERIAL);
			String id = (String) uc.getAttribute(ID);
			String flg = (String)uc.getParameter("flg");
			
			// 根据车辆ID查询竞价信息
			AucManagement aucnamagement = AucManagement.getInstance();
			// 获得车辆竞价信息
			AucBidhistory aucbidhistory = AucBidhistory.getInstance();
			// 获得车辆当前出价最高者信息
			Member member = Member.getInstance();

			// 获得竞价基础信息
			aucnamagement = aucnamagement.getAucInfoBySerialAll(Integer.valueOf(id));
			view.setId(aucnamagement.getId());
			view.setSerial(serial);
			view.setBidStatus(StringUtil.getString(aucnamagement.getBidStatus()));
			view.setBidStatusName(StringUtil.getBidStatus(aucnamagement.getBidStatus()));
			view.setBidType(StringUtil.getBidType(aucnamagement.getBidType()));
			if("0".equals(StringUtil.getPointBetweenThree(aucnamagement.getDonePrices()))){
				view.setDonePrices("");
			}else{
				view.setDonePrices(StringUtil.getPointBetweenThree(aucnamagement.getDonePrices()));
			}
			if (!StringUtil.isNullOrBlank(aucnamagement.getDoneMemberId()))
			{
				view.setDoneMemberId(aucnamagement.getDoneMemberId());
				member = member.findMemberInfoByMemId(aucnamagement.getDoneMemberId());
				view.setDoneMemberName(member.getShortName());
			}
			else
			{
				view.setDoneMemberId("");
				view.setDoneMemberName("");
			}
			if("0".equals(StringUtil.getPointBetweenThree(aucnamagement.getFinalExpetPrices()))){
				view.setFinalExpetPrices("");
			}else{
				view.setFinalExpetPrices(StringUtil.getPointBetweenThree(aucnamagement.getFinalExpetPrices()));
			}
			if (!StringUtil.isNullOrBlank(aucnamagement.getMemberId()))
			{
				view.setMemberId(aucnamagement.getMemberId());
				member = member.findMemberInfoByMemId(aucnamagement.getMemberId());
				view.setFinalExpetMemberName(member.getShortName());
			}
			else
			{
				view.setMemberId("");
				view.setFinalExpetMemberName("");
			}
			view.setStartTime(DateUtil.getTimeByTimeStamp1(aucnamagement.getStartTime()));
			view.setAuctionDeadline(String.valueOf(aucnamagement.getAuctionDeadline()));
			view.setFlg(flg);
			// 出价次数
			view.setBidNum(aucnamagement.getExpetPricesNum());

			if (!StringUtil.isNullOrBlank(aucnamagement.getMemberId())) {
				// 有出价信息
				List<AucBidhistory> aucbidhistoryList = aucbidhistory
						.getMaxAucBidInfoByAucId(aucnamagement.getId());
				List<BidCarSerchAllBidDetailViewHelper> viewhelperList = new ArrayList<BidCarSerchAllBidDetailViewHelper>();
				if (aucbidhistoryList.size() > 0) {	
					
					// 出价列表
					for (int i = 0; i < aucbidhistoryList.size(); i++) {
						BidCarSerchAllBidDetailViewHelper viewhelper = new BidCarSerchAllBidDetailViewHelper();
						AucBidhistory auc = aucbidhistoryList.get(i);
						member = member
								.findMemberInfoByMemId(auc.getMemberId());
						viewhelper.setMemberName(member.getShortName());
						// 商户联系人，联系电话 modify by mufeiyu
						viewhelper.setContactOwner(member.getContactOwner());
						viewhelper.setContactPhone(member.getContactPhone());
						viewhelper.setBidPrice(StringUtil.getPointBetweenThree(auc.getPrice()));
						viewhelper.setBidTime(auc.getBidTime().substring(10, 19));
						// 序号
						viewhelper.setSnumber(i + 1);
						// 式样
						if(i%2 == 0){
							viewhelper.setDectBsFlag("1");
						}else{
							viewhelper.setDectBsFlag("2");
						}
						viewhelperList.add(viewhelper);
					}
				}

				view.setBidList(viewhelperList);
			}

			uc.setAttribute(TmsConstants.VIEWHELP, view);

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}
